<template>
    <div class="addstaff">
        <!-- 头信息 -->
        <Homeheader />
        <!-- 内容区域 -->
        <div class="content">
            <!-- 遮罩层 111-->
            <div class="mask">
                <div class="box">
                    <div class="box1">
                        <div class="one">
                            <div class="one-left">添加员工</div>
                            <div class="one-right">
                                <!-- 点击X，跳转回上一页 -->
                                <el-icon style="font-size: 22px;" @click="router.go(-1)">
                                    <CloseBold />
                                </el-icon>
                            </div>
                        </div>

                        <div class="three" style="display:block" ref="roomref">
                            <div class="text">用户名称</div>
                            <div class="select">
                                <el-input v-model="username" placeholder="请输入名称" style="width: 100%;height: 5vh;" />
                            </div>
                        </div>
                        <div class="three" style="display:block" ref="roomref">
                            <div class="text">手机号</div>
                            <div class="select">
                                <el-input v-model="userphone" placeholder="请输入手机号" style="width: 100%;height: 5vh;" />
                            </div>
                        </div>
                        <div class="three2" style="display:block" ref="roomref">
                            <div class="text">用户角色</div>
                            <div class="select">
                                <el-select v-model="role" placeholder="请选择角色" size="large" style="width: 100%">
                                    <el-option label="收银员" value="收银员" />
                                    <el-option label="服务员" value="服务员" />
                                    <el-option label="厨师" value="厨师" />
                                </el-select>
                            </div>
                        </div>
                        <div class="three" style="display:block" ref="roomref">
                            <div class="text">验证码</div>
                            <div class="select">
                                <el-input v-model="password" placeholder="请输入短信验证码" style="width: 70%;height: 4.8vh;" />
                                <van-button type="primary" size="normal" style="width: 30%;" @click="getcode">
                                    {{ sms }}
                                </van-button>
                            </div>
                        </div>
                        <!-- 按钮 -->
                        <div class="four" @click="addstaff()"
                            :class="(username == '') || (userphone == '') || (password == '') ? '' : 'four-1'">
                            添加
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <!-- 底部导航 -->
        <!-- <Homefooter /> -->
    </div>
</template>
<script setup>
import Homefooter from '@/views/pages/Homefooter.vue';
import Homeheader from '@/views/pages/Homeheader.vue';
import { ref } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import { ElMessage } from 'element-plus'
import axios from 'axios';
let router = useRouter()
let username = ref('')
let userphone = ref('')
let role = ref('')
// 控制验证码按钮展示内容
let sms = ref('获取验证码')
// 控制验证码按钮的状态
let smsflag = ref(false)
// 控制登录按钮
let loginflag = ref(true)
// 验证码内容
let sms_code = ref('')
let password = ref('')
// 添加员工
let addstaff = async () => {
    if (sms_code.value == password.value) {
        await axios.post('http://127.0.0.1:3000/addstaff', {
            username: username.value,
            userphone: userphone.value,
            role: role.value,
        })
            .then(async res => {
                if (res.data.code == 200) {
                    ElMessage({
                        message: '添加员工成功',
                        type: 'success',
                        offset: 150
                    })
                    router.go(-1)
                }else{
                    ElMessage({
                        message: '该手机号已被注册',
                        type: 'error',
                        offset: 150
                    })
                }
            })
    } else if (sms_code.value != password.value) {
        ElMessage({
            message: '验证码输入错误',
            type: 'error',
            offset: 150
        })
        password.value = ''
    }
}
// 获取验证码
let getcode = async () => {
    if (userphone.value == '') {
        return ElMessage({
            message: '请输入您的员工账号',
            type: 'warning',
            offset: 150
        })
    }
    smsflag.value = true
    sms.value = '60s'
    await axios.post('http://127.0.0.1:3000/sms/get_sms', { phone: userphone.value })
        .then(res => {
            if (res.status == 200) {
                // console.log(res.data);
                // console.log(res.data.sms_code);
                ElMessage({
                    message: '验证码发送成功',
                    type: 'success',
                    offset: 150
                })
                sms_code.value = res.data.sms_code
                timeinterval(59)
            }
        })
}
// 定时器
function timeinterval(time) {
    let timer = setInterval(() => {
        if (time > 0) {
            sms.value = time + 's'
            time--
        } else {
            clearInterval(timer)
            sms.value = '获取验证码'
            smsflag.value = false
        }
    }, 1000)
}

</script>
<style scoped lang="less">
.addstaff {
    padding-top: 10vh;
    box-sizing: border-box;

    .content {
        background-color: #F5F5F5;
        height: 90vh;
        width: 100vw;
        padding: 2vh 4vh;
        box-sizing: border-box;

        //   遮罩层
        .mask {
            width: 100%;
            height: 100%;
            background-color: rgba(0, 0, 0, 0.5);
            position: absolute;
            top: 0;
            left: 0;

            .box {
                width: 35%;
                height: 63vh;
                background-color: #fff;
                position: absolute;
                top: 55%;
                left: 50%;
                transform: translate(-50%, -50%);
                border-radius: 12px;

                .box1 {
                    width: 90%;
                    height: 90%;

                    position: absolute;
                    top: 50%;
                    left: 50%;
                    transform: translate(-50%, -50%);

                    .one {
                        width: 100%;
                        height: 5vh;

                        display: flex;
                        justify-content: space-between;
                        align-items: center;

                        .one-left {
                            width: 38%;
                            height: 100%;

                            font-size: 28px;
                            color: black;
                            line-height: 5vh;
                            font-weight: 800;
                        }

                        .one-right {
                            width: 7%;
                            height: 56%;
                        }
                    }

                    .three {
                        width: 100%;
                        height: 9vh;
                        margin-top: 2vh;

                        .text {
                            width: 100%;
                            height: 3vh;

                            font-size: 17px;
                            color: black;
                            font-weight: 600;
                            line-height: 3vh;
                        }

                        .select {
                            width: 100%;
                            height: 5vh;
                            margin-top: 1vh;

                            border-radius: 10px;
                            background-color: #f5f5f5;
                            display: flex;
                            justify-content: flex-start;
                            align-items: center;

                        }
                    }

                    .three2 {
                        width: 100%;
                        height: 9vh;
                        margin-top: 2vh;

                        .text {
                            width: 100%;
                            height: 3vh;

                            font-size: 17px;
                            color: black;
                            font-weight: 600;
                            line-height: 3vh;
                        }

                        .select {
                            width: 100%;
                            height: 5vh;
                            margin-top: 1vh;

                            border-radius: 10px;
                            display: flex;
                            justify-content: flex-start;
                            align-items: center;

                        }
                    }

                    .four {
                        width: 100%;
                        height: 6vh;

                        margin-top: 2vh;
                        border-radius: 12px;
                        text-align: center;
                        line-height: 6vh;
                        font-size: 19px;
                        background: #b4dbfd;
                        color: white;
                    }

                    .four-1 {
                        background: #2065a1;

                    }
                }
            }
        }
    }
}
</style>